package com.mfhcd.facepay.utils;


import com.mfhcd.facepay.constant.SignConstant;
import com.mfhcd.facepay.model.BaseRequestModel;
import com.mfhcd.facepay.model.BaseResponseModel;

import java.util.Map;
import java.util.TreeMap;

/**
 * @Author：张洪顺
 * @E-Mail: zhanghs@mfhcd.com
 * @Date：2019-10-14 13:16
 * @版权所有 © 现代金融控股（成都）有限公司
 * @描述：签名工具类
 */
public class SignatureUtil {
    /**
     * saas验签
     *
     * @param sign 请求签名
     * @param reqT request body ReqT
     * @return boolean
     */
    public static boolean saasVerify(String sign, BaseRequestModel reqT) {
        // 临时设置一个万能签名
        if ("000000".equals(sign)) {
            return true;
        }
        if (null == sign || null == reqT) {
            return false;
        }
        String s = saasSign(reqT);
        return sign.equals(s);
    }
    /**
     * paas验签
     *
     * @param sign 请求签名
     * @param reqT request body ReqT
     * @return boolean
     */
    public static boolean paasVerify(String sign, BaseRequestModel reqT) {
        // 临时设置一个万能签名
        if ("000000".equals(sign)) {
            return true;
        }
        if (null == sign || null == reqT) {
            return false;
        }
        String s = paasSign(reqT);
        return sign.equals(s);
    }
    /**
     * saas验签
     *
     * @param sign 请求签名
     * @param resT response body resT
     * @return boolean
     */
    public static boolean saasVerify(String sign, BaseResponseModel resT) {
        if (null == sign || null == resT) {
            return false;
        }
        String s = saasSign(resT);
        return sign.equals(s);
    }
    /**
     * paas验签
     *
     * @param sign 请求签名
     * @param resT response body resT
     * @return boolean
     */
    public static boolean paasVerify(String sign, BaseResponseModel resT) {
        if (null == sign || null == resT) {
            return false;
        }
        String s = paasSign(resT);
        return sign.equals(s);
    }
    /**
     * Saas请求加签
     *
     * @param param req.param
     * @return String
     */
    public static String saasSign(BaseRequestModel param) {
        if (null == param) {
            return null;
        }
        TreeMap<String, String> treeMap = new TreeMap<>();
        String hostName = param.getHost();
        if (null != hostName && !hostName.isEmpty()) {
            treeMap.put(SignConstant.SAAS_SIGN_HOST, hostName);
        }
        String sysCode = param.getService();
        if (null != sysCode && !sysCode.isEmpty()) {
            treeMap.put(SignConstant.SAAS_SIGN_SERVICE, sysCode);
        }
        String requestCode = param.getApi();
        if (null != requestCode && !requestCode.isEmpty()) {
            treeMap.put(SignConstant.SAAS_SIGN_API, requestCode);
        }
        return saasSign(treeMap);
    }

    /**
     * Paas请求加签
     *
     * @param param req.param
     * @return String
     */
    public static String paasSign(BaseRequestModel param) {
        if (null == param) {
            return null;
        }
        TreeMap<String, String> treeMap = new TreeMap<>();
        String hostName = param.getHost();
        if (null != hostName && !hostName.isEmpty()) {
            treeMap.put(SignConstant.PAAS_SIGN_HOST, hostName);
        }
        String sysCode = param.getService();
        if (null != sysCode && !sysCode.isEmpty()) {
            treeMap.put(SignConstant.PAAS_SIGN_SERVICE, sysCode);
        }
        String requestCode = param.getApi();
        if (null != requestCode && !requestCode.isEmpty()) {
            treeMap.put(SignConstant.PAAS_SIGN_API, requestCode);
        }
        return paasSign(treeMap);
    }
    /**
     * saas响应加签
     *
     * @param saaSResp saaSResp
     * @return String
     */
    public static String saasSign(BaseResponseModel saaSResp) {
        if (null == saaSResp) {
            return null;
        }
        TreeMap<String, String> treeMap = new TreeMap<>();
        String procCode = saaSResp.getCode();
        if (null != procCode && !procCode.isEmpty()) {
            treeMap.put("procCode", procCode);
        }
        String procDesc = saaSResp.getMessage();
        if (null != procDesc && !procDesc.isEmpty()) {
            treeMap.put("procDesc", procDesc);
        }
        String sysCode = saaSResp.getSystem();
        if (null != sysCode && !sysCode.isEmpty()) {
            treeMap.put("sysCode", sysCode);
        }
        return saasSign(treeMap);
    }

    /**
     * 响应加签
     *
     * @param paaSResp saaSResp
     * @return String
     */
    public static String paasSign(BaseResponseModel paaSResp) {
        if (null == paaSResp) {
            return null;
        }
        TreeMap<String, String> treeMap = new TreeMap<>();
        String procCode = paaSResp.getCode();
        if (null != procCode && !procCode.isEmpty()) {
            treeMap.put("procCode", procCode);
        }
        String procDesc = paaSResp.getMessage();
        if (null != procDesc && !procDesc.isEmpty()) {
            treeMap.put("procDesc", procDesc);
        }
        String sysCode = paaSResp.getSystem();
        if (null != sysCode && !sysCode.isEmpty()) {
            treeMap.put("sysCode", sysCode);
        }
        return paasSign(treeMap);
    }

    /**
     * saas签名
     *
     * @param treeMap 参数 TreeMap
     * @return String
     */
    public static String saasSign(TreeMap<String, String> treeMap) {
        if (null == treeMap || treeMap.size() == 0) {
            return null;
        }
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(SignConstant.SAAS_SIGN_SALT_0);
        StringBuilder keyValueJoiner;
        for (Map.Entry<String, String> entry : treeMap.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            if (null != key && !key.isEmpty() && null != value && !value.isEmpty()) {
                stringBuilder.append(SignConstant.PARAMS_STRING_DELIMITER);
                keyValueJoiner = new StringBuilder();
                keyValueJoiner.append(key);
                keyValueJoiner.append(SignConstant.PARAMS_KEY_VALUE_DELIMITER);
                keyValueJoiner.append(value);
                stringBuilder.append(keyValueJoiner.toString());
            }
        }
        stringBuilder.append(SignConstant.PARAMS_STRING_DELIMITER);
        stringBuilder.append(SignConstant.SAAS_SIGN_SALT_1);
        return SignEncryptUtil.sha256(stringBuilder.toString());
    }

    /**
     * paas签名
     *
     * @param treeMap 参数 TreeMap
     * @return String
     */
    public static String paasSign(TreeMap<String, String> treeMap) {
        if (null == treeMap || treeMap.size() == 0) {
            return null;
        }
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(SignConstant.PAAS_SIGN_SALT_0);
        StringBuilder keyValueJoiner;
        for (Map.Entry<String, String> entry : treeMap.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            if (null != key && !key.isEmpty() && null != value && !value.isEmpty()) {
                stringBuilder.append(SignConstant.PARAMS_STRING_DELIMITER);
                keyValueJoiner = new StringBuilder();
                keyValueJoiner.append(key);
                keyValueJoiner.append(SignConstant.PARAMS_KEY_VALUE_DELIMITER);
                keyValueJoiner.append(value);
                stringBuilder.append(keyValueJoiner.toString());
            }
        }
        stringBuilder.append(SignConstant.PARAMS_STRING_DELIMITER);
        stringBuilder.append(SignConstant.PAAS_SIGN_SALT_1);
        return SignEncryptUtil.sha256(stringBuilder.toString());
    }


}
